package com.mmy.controller;

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mmy.domain.sys.TSysUser;
import com.mmy.util.Result;
import com.mmy.util.ResultCode;
import lombok.extern.slf4j.Slf4j;

/**
 * @vesion 
 * @description 测试
 * @author liys
 * @date 2021-5-31
 */

@Controller
@RequestMapping("/test")
@Slf4j
public class TestController {
	
	@RequestMapping("/info")
	public String testPage(HttpServletRequest request,Model model) {
		
		String url = request.getSession().getServletContext().getRealPath("/upload");
		model.addAttribute("path",url);
     	model.addAttribute("version", "2021-07-08");
     	model.addAttribute("bugs","1.新建旅行社后,后台没有新建的信息");
     	model.addAttribute("description","后台样式-旅游部分修改完成");
		return "test";
	}
	
	@PostMapping("/{id}")
	@ResponseBody
	public Api testPost(@PathVariable("id")String id) {
		log.info("post" + id);
		Api api = new Api();
		api.setCode(200);
		api.setMsg("成功");
		return api;
	}
	
	@GetMapping("/{id}")
	@ResponseBody
	public JSONObject testGet(@PathVariable("id")String id) {
		log.info("get" + id);
		return JSON.parseObject("{\"message\":\"SUCCESS\",\"code\":200}");
	}
	
	@PostMapping("/{a}/{b}/{c}")
	@ResponseBody
	public String testRestful(@PathVariable("a")String a,@PathVariable("b")String b,@PathVariable("c")String c) {
		
		StringBuffer sb = new StringBuffer();
		sb.append(a);
		sb.append(b);
		sb.append(c);
		
		return sb.toString();
	}
	
	@RequestMapping("/sucess")
	@ResponseBody
	public Result testRes() {
		
		return Result.success();
	}
	
	@RequestMapping("/false")
	@ResponseBody
	public Result testRef() {
		
		TSysUser user = new TSysUser();
		user.setUserName("马老师");
		user.setUserMobile("10086");
		
		return Result.failure(ResultCode.PARAM_IS_INVALID,user);
	}
	
	@RequestMapping("/j/{num}")
	@ResponseBody
	public Result testRess(@PathVariable int num) {
		
		Result result = new Result();
		
		switch (num) {
		case 0:
			result.setResultCode(ResultCode.USER_NOT_EXIST);
			break;
		case 1:
			result.setResultCode(ResultCode.SYSTEM_INNER_ERROR);
			break;
		default:
			result.setResultCode(ResultCode.PARAM_IS_INVALID);
			break;
		}
		
		return result;
	}
	
	@RequestMapping("/json")
	@ResponseBody
	public JSONObject testJson() {
		
		JSONObject json = new JSONObject();
		TSysUser user = new TSysUser();
		user.setUserName("马老师");
		user.setUserMobile("10086");
		json.put("暗号", "96965588");
		json.put("广播", "3721");
		json.put("dataaaa", user);
		
		return json;
	}
	class Api{
		private int code;
		private String msg;
		public int getCode() {
			return code;
		}
		public void setCode(int code) {
			this.code = code;
		}
		public String getMsg() {
			return msg;
		}
		public void setMsg(String msg) {
			this.msg = msg;
		}
	}
}
